import { defineStore } from 'pinia';

export const useLoginStore = defineStore('loginStore', {
	state: () => {
		return {
            showWxLogin: false,
			userInfo: {
              // avatar: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
              // createBy: null,
              // createDeptId: null,
              // createTime: "2024-08-16 22:04:27",
              gender: "0",
              id: 2,
              isBinding: "0",
              nickName: "微信用户",
              openId: "o9o0a7amSnZUb6y4DD2pbLKhlcHM",
              remark: "",
              // sysUserId: null,
              // sysUserName: null,
              // token: null,
              // updateBy: null,
              // updateTime: null,
			}, // 登录人信息
            accessToken: '', // 萤石token
			isLogin: false, // fasle: 未登录
			pageUrl: '/pages/home/index'
		};
	},
	actions: {
        setShowWxLogin(bool) {
          bool ? uni.hideTabBar() : uni.showTabBar();
          this.showWxLogin = bool;
        },
		// 保存登录的必要信息
        setUserInfo(userInfo) {
          this.userInfo = { ...this.userInfo, ...userInfo };
          this.isLogin = this.userInfo.isBinding === "1";
        },
        // 设置AccessToken
        async setAccessToken() {
          const res = await uni.$api.getAccessToken();
          this.accessToken = res.msg;
        },
		// 退出登录
		async logout() {
			this.$reset()
			uni.clearStorageSync();
			uni.reLaunch({
				url: '/pages/login/index'
			});
		}
	},
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
        getItem(key) {
          return uni.getStorageSync(key)
        },
      },
    }
});
